package com.zymail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyMap;
import com.zymail.bean.ZyMoney;
import com.zymail.bean.ZyOrder;
import com.zymail.bean.ZyUser;
import com.zymail.entity.WxPayNotifyV0;
import com.zymail.service.ZyMapService;
import com.zymail.service.ZyMoneyService;
import com.zymail.service.ZyOrderService;
import com.zymail.service.ZyUserService;
import com.zymail.wxpay.WXPayUtil;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zymail.service.impl.WxPayService;

@Log4j
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{
    @Autowired
    public ZyOrderService orderService;

    @Autowired
    public ZyUserService userService;

    @Autowired
    public ZyMoneyService moneyService;

    @Autowired
    public WxPayService payService;

    @Autowired
    public ZyMapService mapService;

    /**
     * 得到用户的所有order
     * @param session_key session_key
     * @return  List<ZyOrder>
     */
    @GetMapping("/getmyorders/{session_key}")
    public List<ZyOrder> getlist(@PathVariable("session_key") String session_key){
        Integer userId = get_userId(session_key);
        if(userId == null) return null;
        return orderService.get_one_order(userId);
    }

    /**
     * 得到order_id对应的order
     * @param session_key session_key
     * @param order_id 需要查询的order_id
     * @return  order实体
     */
    @GetMapping("/getorder/{session_key}/{order_id}")
    public ZyOrder getorder(@PathVariable("session_key") String session_key, @PathVariable("order_id") Integer order_id){
        Integer userId = get_userId(session_key);
        ZyOrder order = orderService.getById(order_id);
        if(order == null || userId == 0 || !userId.equals(order.getUserId()))return null;
        return order;
    }

    /**
     * 删除order
     * @param session_key session_key
     * @param order_id  需要删除的order
     * @return  boolean
     */
    @DeleteMapping("/delorder/{session_key}/{order_id}")
    public boolean delorder(@PathVariable("session_key") String session_key, @PathVariable("order_id") Integer order_id){
        Integer userId = get_userId(session_key);
        ZyOrder order = orderService.getById(order_id);
        if(order == null || userId == 0 || !userId.equals(order.getUserId())) return false;
        return orderService.removeById(order_id);
    }

    /**
     * 取消订单
     * @param session_key   session_key
     * @param orderId   需要取消订单的用户id
     * @return  bool
     */
    @DeleteMapping("/cancelOrder/{session_key}/{order_id}")
    public boolean cancelOrder(@PathVariable("session_key") String session_key,
                               @PathVariable("order_id") Integer orderId){
        Integer userId = get_userId(session_key);
        if(userId == 0) return false;

        ZyOrder order = orderService.getById(orderId);
        if(!order.getUserId().equals(userId)) return false;

        order.setUser(null);
        order.setGoodDe(null);
        order.setFloor(null);
        order.setFlag(7);   //已取消

        return orderService.updateById(order);
    }

    /**
     * 完成订单
     * @param session_key   session_key
     * @param orderId   需要完成的订单id
     * @return  bool
     */
    @PutMapping("/completeOrder/{session_key}/{order_id}")
    public boolean completeOrder(@PathVariable("session_key") String session_key,
                                 @PathVariable("order_id") Integer orderId) throws Exception {
        Integer userId = get_userId(session_key);
        if(userId == 0) return false;

        ZyOrder order = orderService.getById(orderId);
        if(!order.getUserId().equals(userId)) return false;

        order.setUser(null);
        order.setGoodDe(null);
        order.setFloor(null);
        order.setFlag(1); //已完成

       Double fa_1 = Double.parseDouble(mapService.getvalue("fa_1"));
       Double fa_2 = Double.parseDouble(mapService.getvalue("fa_2"));
       Double fa_3 = Double.parseDouble(mapService.getvalue("fa_3"));

       List<Integer> list = userService.getroot(userId);

       if(list.size() >= 1){
           //给爸爸发钱
           payService.wxrePay(list.get(0), orderId, order.getPrice() * fa_1);
       }

       if(list.size() >= 2){
           //给爷爷发钱
           payService.wxrePay(list.get(1), orderId, order.getPrice() * fa_2);
       }

       if(list.size() >= 3){
           //给曾爷爷发钱
           payService.wxrePay(list.get(2), orderId, order.getPrice() * fa_3);
       }


        return orderService.updateById(order);
    }

    /**
     * 返回分类列表的所有list
     * @return
     */
    @RequestMapping("/getall")
    public Map<String,Object> getAlllist(){
        Map<String,Object> map = new HashMap<>();
        List<ZyOrder> data = orderService.list();
        map.put("code",0);
        map.put("data",data);
        return map;
    }
}
